<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader"
                class="searchHeader">

                <el-form-item label="所属集团" prop="pkGroup">
                    <ec-group-tree-select ref="groupTreeSelect" v-model="searchForm.pkGroup"></ec-group-tree-select>
                </el-form-item>

                <el-form-item label="所属组织" prop="pkOrg">
                    <ec-org-tree-select ref="orgTreeSelect" v-model="searchForm.pkOrg" :pkGroup="searchForm.pkGroup">
                    </ec-org-tree-select>
                </el-form-item>

                <el-form-item label="下级用户组" prop="pid">
                    <ec-usergroup-tree-select ref="usergroupTreeSelect" v-model="searchForm.pid"
                        :pkGroup="searchForm.pkGroup" :pkOrg="searchForm.pkOrg" placeholder="请选择用户组">
                    </ec-usergroup-tree-select>
                </el-form-item>

                <el-form-item label="用户组名称" prop="usergroupName">
                    <el-input v-model="searchForm.usergroupName" placeholder="请输入用户组名称" clearable />
                </el-form-item>
                <el-form-item label="操作">
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()">添加
                </el-button>
                <el-button type="primary" icon="el-icon-caret-top" size="small" v-if="!tigger"
                    @click="tableExpandTigger()">展开</el-button>
                <el-button type="primary" icon="el-icon-caret-bottom" size="small" v-else @click="tableExpandTigger()">
                    合并
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteHandle()"
                    :disabled="!multipleSelection.length >0" v-if="isAuth()">批量删除
                    <span v-if="multipleSelection.length >0">【{{multipleSelection.length}}条】</span>
                </el-button>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download" @click="dataImport()"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2" @click="dataExport()"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tabledata" ref="dataTable" row-key="pkUsergroup"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}" v-loading="loading"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="groupName" header-align="center" align="center" show-overflow-tooltip label="所属集团">
            </el-table-column>
            <el-table-column prop="orgName" header-align="center" align="center" show-overflow-tooltip label="所属组织">
            </el-table-column>
            <el-table-column prop="parentUsergroupName" header-align="center" align="center" show-overflow-tooltip
                label="上级用户组">
            </el-table-column>
            <el-table-column prop="usergroupName" header-align="center" align="center" show-overflow-tooltip
                label="用户组名称">
                <template slot-scope="scope">
                    <span style="color:#409EFF;cursor: pointer;"
                        @click="manageHandle(scope.row.pkUsergroup)">{{scope.row.usergroupName}}</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="manageHandle(scope.row.pkUsergroup)">管理
                    </el-button>
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.pkUsergroup)">修改
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.pkUsergroup)">删除</el-button>
                </template>
            </el-table-column>

        </ec-table>

        <!-- 表格表尾 -->
        <template slot="footer">
            <!-- <el-button type="primary" size="mini" :loading="uploading" style="float:left;">
                <d2-icon name="cloud-upload" />
                Upload log data
            </el-button> -->

            <!-- <el-pagination :current-page="page.current" :page-size="page.size" :total="page.total"
                :page-sizes="[10,50,100]" layout="total, sizes, prev, pager, next, jumper" style="float:right;"
                @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </el-pagination> -->
        </template>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>

        <!-- <manage v-if="manageVisible" ref="manage" @refreshDataList="getDataTable"></manage> -->
        <authorize v-if="authorizeVisible" ref="authorize" @refreshDataList="getDataTable"></authorize>

        <!-- Excel导入 -->
        <ec-import ref="ecimport" @refreshDataList="getDataTable" code="usergroup" url="/report/usergroup/import">
        </ec-import>
    </d2-container>
</template>

<script>
    import * as usergroupService from '@/api/upms/usergroup'
    import AddOrUpdate from './add-or-update'
    // import Manage from './manage'
    import Authorize from './authorize'
    import util from '@/libs/util'
    export default {
        name: "upms-usergroup-index",
        data() {
            return {
                searchForm: {
                    pid: '',
                    usergroupName: '',
                    pkGroup: '',
                    pkOrg: '',
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                tigger: false,
                showHeader: true,
                tabledata: [],
                multipleSelection: [],
                loading: false,
                addOrUpdateVisible: false,
                manageVisible: false,
                authorizeVisible: false
            }
        },
        components: {
            AddOrUpdate,
            Authorize,
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    // page: this.page.page,
                    // limit: this.page.limit,
                    pid: this.searchForm.pid,
                    usergroupName: this.searchForm.usergroupName,
                    pkGroup: this.searchForm.pkGroup,
                    pkOrg: this.searchForm.pkOrg,
                };

                usergroupService.getusergrouptree(query).then(data => {
                    this.tabledata = data;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                })
            },
            // 切换管理页面
            manageHandle(id) {
                // console.log('111')
                this.authorizeVisible = true
                this.$nextTick(() => {
                    this.$refs.authorize.init(id)
                })
            },
            //切换页数
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            // 翻页
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
                this.$refs.groupTreeSelect.resetFields()
                this.$refs.orgTreeSelect.resetFields()
                this.$refs.usergroupTreeSelect.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            }, // 多选事件
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 删除/批量删除
            deleteHandle(id) {
                var ids = id ? [id] : this.multipleSelection.map(item => {
                    return item.pkUsergroup
                })
                if (ids.length > 0) {
                    this.$confirm("确认删除[" + ids.length + "]条数据？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        usergroupService.delBatchusergroup(ids).then(() => {
                            this.$message.success("删除成功！");
                            this.getDataTable();
                        });
                    });
                } else {
                    this.$message.error("请选择数据！")
                }
            },
            dataImport() {
                this.$nextTick(() => {
                    this.$refs.ecimport.init()
                })
            },
            // 上传Excel文件
            uploadExcelHandler(file) {
                if (!file) {
                    this.$message.error("文件错误！")
                }

                let formData = new FormData() // 创建form对象 
                formData.append('file', file);
                formData.append('ceshi', '1');
                console.log(formData)
                usergroupService.usergroupImport(formData).then(data => {
                    console.log(data)
                })

            },
            dataExport() {
                let query = {
                    page: 1,
                    limit: 500,
                    pid: this.searchForm.pid,
                    usergroupName: this.searchForm.usergroupName,
                    pkGroup: this.searchForm.pkGroup,
                    pkOrg: this.searchForm.pkOrg,
                };
                usergroupService.usergroupExport(query).then(data => {
                    this.$message.success(data)
                })
            },
            // 展开/合并
            tableExpandTigger() {
                this.tigger = !this.tigger
                let rows = this.tabledata
                this.dg(rows, this.tigger)
            },
            dg(rows, status) {
                let that = this
                rows.forEach((item, index) => {
                    if (item.children) {
                        that.dg(item.children, status)
                        this.$refs.dataTable.$refs.elTable.toggleRowExpansion(item, status)
                    }
                })
            },
        }
    }
</script>